package com.pavelk.orion.model
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.net.URLRequest;
	import flash.events.*;
	//import flash.utils.*;
	//import flash.geom.ColorTransform;
		
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;
    import org.puremvc.as3.utilities.loadup.interfaces.ILoadupProxy;
    
    import com.pavelk.orion.ApplicationFacade;
    
    import com.pavelk.orion.model.vo.AssetsVO;
	
	public class RuntimeAssetsProxy extends EntityProxy implements ILoadupProxy
	{
		public static const NAME  :String = "RuntimeAssetsProxy";
        public static const SRNAME:String = "RuntimeAssetsSRProxy";
		
		public function RuntimeAssetsProxy()
		{
			super( NAME, new Object() );
		}
		
		public function load() : void
		{
			sendNotification( ApplicationConstants.ASSETS_LOADING );
			sendNotification( ApplicationConstants.LOADING_TYPE, "percentView" );
			
			var path:URLRequest = new URLRequest( "SiteAssets.swf" );
			var context:LoaderContext = new LoaderContext( false, ApplicationDomain.currentDomain );
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, runtimeAssetsLoadComplete );
			loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgress );
			loader.load( path, context );
		}
		
		private function onProgress( e:ProgressEvent ) : void
		{
			sendNotification( ApplicationConstants.PROGRESS_ASSETS, Math.ceil( e.bytesLoaded / e.bytesTotal *100).toString() );
		}
		
		private function runtimeAssetsLoadComplete( e:Event ) : void 
		{				
			for each( var asset:String in e.target.content.getAssets() )
			{	
				var vo         :AssetsVO = new AssetsVO( asset );
				var className  :String   = asset;
				var splicePoint:Number   = className.lastIndexOf(".") + 1;
				var shortName  :String   = splicePoint ? className.substr(splicePoint) : className;
				
				data[ shortName ] = vo;
			}			
			
			sendLoadedNotification( ApplicationConstants.ASSETS_LOADED, NAME, SRNAME );
			sendNotification( ApplicationConstants.ASSETS_DATA, data );
		}
		
		public function getAssets() : void
		{
			//trace("Jede to? " + a);
			sendNotification( ApplicationConstants.RESULT_ASSETS, data );
			/*
			for each( var key:String in a )
			{
				trace(key);
				trace( "assets " + key in data );
				if( data[key] == key )
				{
					trace( "assets " + data.asset );
				}
			}
			*/
			

		}

	}
}